﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UrlChecker.Setting
{
    public partial class Option : Form
    {
        #region Atrributes

        public static List<string> Sids = new List<string>();

        #endregion

        public Option()
        {
            InitializeComponent();
        }

        private void Option_Load(object sender, EventArgs e)
        {
            InitOption();
        }

        #region Event

        #region Button

        private void btnSearchEngine_Click(object sender, EventArgs e)
        {
            SearchList mySearch = new SearchList();
            mySearch.SearchSelections = Sids;
            mySearch.Owner = this;
            mySearch.SearchRefresh += new SearchList.ButtonHanlder(SearchRefresh);
            mySearch.ShowDialog();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            bool b = true;
            if (!QT.SEO.Setting.Update("SearchEngine", string.Join("|", Sids.ToArray()))) b = false;
            if (!QT.SEO.Setting.Update("DefaultPage", this.tbDefaultPages.Text)) b = false;
            if(!QT.SEO.Setting.Update("InterVal",this.nudInterVal.Value.ToString())) b=false;
            if(!b) 
            {
                MessageBox.Show("设置失败");
                return;
            }
            this.Close();
        }

        #endregion

        #endregion

        #region Method

        public void SearchRefresh(List<string> SearchSelections, List<string> SearchNames)
        {
            this.tbSearchEngine.Text = string.Join(";", SearchNames.ToArray());
            Sids = SearchSelections;
            if (this.tbSearchEngine.Text.Length > 0)
            {
                this.tbSearchEngine.Visible = true;
                this.btnSearchEngine.Location = new Point(this.tbSearchEngine.Location.X + this.tbSearchEngine.Width + 2, this.btnSearchEngine.Location.Y);
            }
            else
            {
                this.tbSearchEngine.Visible = false;
                this.btnSearchEngine.Location = new Point(this.tbSearchEngine.Location.X, this.btnSearchEngine.Location.Y);
            }
        }

        private void InitOption()
        {
            Sids = new List<string>(QT.SEO.Setting.Get("SearchEngine").Split('|'));
            List<string> Snames = new List<string>();

            foreach (string sid in Sids)
            {
                DataTable dt = QT.SEO.SearchEngine.Get(sid);
                if (dt.Rows.Count == 1) Snames.Add(dt.Rows[0]["SearchName"].ToString());
            }

            this.tbSearchEngine.Text = string.Join(";", Snames.ToArray());
            if (this.tbSearchEngine.Text.Length > 0)
            {
                this.tbSearchEngine.Visible = true;
                this.btnSearchEngine.Location = new Point(this.tbSearchEngine.Location.X + this.tbSearchEngine.Width + 2, this.btnSearchEngine.Location.Y);
            }
            this.tbDefaultPages.Text = QT.SEO.Setting.Get("DefaultPage");
            this.nudInterVal.Value = decimal.Parse(QT.SEO.Setting.Get("InterVal"));
        }

        #endregion
    }
}
